package Opgave1;

import Opgave1.RunnableThread.Thread1;
import Opgave1.RunnableThread.Thread2;

public class JoinOpgave{
	public static void main( String[ ] args ){
		System.out.println( "main started" );

		Thread1 thread1 = new RunnableThread.Thread1();
		final Thread t1 = new Thread(thread1);
		
		System.out.println( "t1 made" );

		Thread2 thread2 = new RunnableThread.Thread2(t1);
		final Thread t2 = new Thread(thread2);
		System.out.println( "t2 made" );

		t1.start( );
		t2.start( );

		try{ 
			t2.join( ); 
		}
		catch( InterruptedException e ){ 
		}				

		System.out.println( "main finishes" );
	}
}

abstract class RunnableThread {
	static class Thread1 implements Runnable{
		public void run() {
			System.out.println( "t1 started" );
			try{ 
				Thread.sleep( 2000 );
			}
			catch( InterruptedException e ){ 
			}				
			System.out.println( "t1 finishes" );	
		}
	}
	
	static class Thread2 implements Runnable{
		Thread t1;
		public Thread2(Thread t1) {
			this.t1 = t1;
		}
		
		public void run() {
			System.out.println( "t2 started" );
			try{ 
				t1.join( ); 
			}
			catch( InterruptedException e ){ 
			}				
			System.out.println( "t2 finishes" );	
		}
	}
}
